package com.ims.core.doc.service.impl;

import java.util.List;
import com.ims.common.utils.DateUtils;
import com.ims.common.utils.uuid.IdUtils;
import com.ims.system.domain.TSysSite;
import com.ims.system.service.ITSysSiteService;
import com.ims.system.service.impl.TSysSiteServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ims.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ims.core.doc.domain.TInDeductionApplyEntry;
import com.ims.core.doc.mapper.TInDeductionApplyMapper;
import com.ims.core.doc.domain.TInDeductionApply;
import com.ims.core.doc.service.ITInDeductionApplyService;

/**
 * 抵扣发票勾选抵扣申请Service业务层处理
 *
 * @author IMS
 * @date 2024-12-03
 */
@Service
public class TInDeductionApplyServiceImpl implements ITInDeductionApplyService
{
    @Autowired
    private ITSysSiteService tSysSiteService;

    @Autowired
    private TInDeductionApplyMapper tInDeductionApplyMapper;

    /**
     * 查询抵扣发票勾选抵扣申请
     *
     * @param id 抵扣发票勾选抵扣申请主键
     * @return 抵扣发票勾选抵扣申请
     */
    @Override
    public TInDeductionApply selectTInDeductionApplyByDeductionId(String deductionId)
    {
        return tInDeductionApplyMapper.selectTInDeductionApplyByDeductionId(deductionId);
    }

    /**
     * 查询抵扣发票勾选抵扣申请
     *
     * @param id 抵扣发票勾选抵扣申请主键
     * @return 抵扣发票勾选抵扣申请
     */
    @Override
    public TInDeductionApply selectTInDeductionApplyById(Long id)
    {
        return tInDeductionApplyMapper.selectTInDeductionApplyById(id);
    }

    /**
     * 查询抵扣发票勾选抵扣申请列表
     *
     * @param tInDeductionApply 抵扣发票勾选抵扣申请
     * @return 抵扣发票勾选抵扣申请
     */
    @Override
    public List<TInDeductionApply> selectTInDeductionApplyList(TInDeductionApply tInDeductionApply)
    {
        return tInDeductionApplyMapper.selectTInDeductionApplyList(tInDeductionApply);
    }

    /**
     * 新增抵扣发票勾选抵扣申请
     *
     * @param tInDeductionApply 抵扣发票勾选抵扣申请
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertTInDeductionApply(TInDeductionApply tInDeductionApply)
    {
        tInDeductionApply.setDeductionId(IdUtils.simpleUUID());
        tInDeductionApply.setCreateTime(DateUtils.getNowDate());
        int rows = tInDeductionApplyMapper.insertTInDeductionApply(tInDeductionApply);
        insertTInDeductionApplyEntry(tInDeductionApply);
        return rows;
    }

    /**
     * 修改抵扣发票勾选抵扣申请
     *
     * @param tInDeductionApply 抵扣发票勾选抵扣申请
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateTInDeductionApply(TInDeductionApply tInDeductionApply)
    {
        tInDeductionApply.setUpdateTime(DateUtils.getNowDate());
        tInDeductionApplyMapper.deleteTInDeductionApplyEntryByDeductionId(tInDeductionApply.getDeductionId());
        insertTInDeductionApplyEntry(tInDeductionApply);
        return tInDeductionApplyMapper.updateTInDeductionApply(tInDeductionApply);
    }


    /**
     * 修改抵扣发票勾选抵扣申请明细
     *
     * @param tInDeductionApplyEntry 抵扣发票勾选抵扣申请明细
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateTInDeductionApplyEntry(TInDeductionApplyEntry tInDeductionApplyEntry)
    {
        tInDeductionApplyEntry.setUpdateTime(DateUtils.getNowDate());
        return tInDeductionApplyMapper.updateTInDeductionApplyEntry(tInDeductionApplyEntry);
    }


    /**
     * 修改抵扣发票勾选抵扣申请
     *
     * @param tInDeductionApply 抵扣发票勾选抵扣申请
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateTInDeductionApplyOnly(TInDeductionApply tInDeductionApply)
    {
        tInDeductionApply.setUpdateTime(DateUtils.getNowDate());
        return tInDeductionApplyMapper.updateTInDeductionApply(tInDeductionApply);
    }

    /**
     * 批量删除抵扣发票勾选抵扣申请
     *
     * @param ids 需要删除的抵扣发票勾选抵扣申请主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTInDeductionApplyByIds(Long[] ids)
    {
        tInDeductionApplyMapper.deleteTInDeductionApplyEntryByDeductionIds(ids);
        return tInDeductionApplyMapper.deleteTInDeductionApplyByIds(ids);
    }

    /**
     * 删除抵扣发票勾选抵扣申请信息
     *
     * @param id 抵扣发票勾选抵扣申请主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTInDeductionApplyById(Long id)
    {
        TInDeductionApply tInDeductionApply = selectTInDeductionApplyById(id);
        tInDeductionApplyMapper.deleteTInDeductionApplyEntryByDeductionId(tInDeductionApply.getDeductionId());
        return tInDeductionApplyMapper.deleteTInDeductionApplyById(id);
    }

    /**
     * 新增抵扣发票勾选抵扣申请明细信息
     *
     * @param tInDeductionApply 抵扣发票勾选抵扣申请对象
     */
    public void insertTInDeductionApplyEntry(TInDeductionApply tInDeductionApply)
    {
        List<TInDeductionApplyEntry> tInDeductionApplyEntryList = tInDeductionApply.getTInDeductionApplyEntryList();
        String deductionId = tInDeductionApply.getDeductionId();
        if (StringUtils.isNotNull(tInDeductionApplyEntryList))
        {
            List<TInDeductionApplyEntry> list = new ArrayList<TInDeductionApplyEntry>();
            for (TInDeductionApplyEntry tInDeductionApplyEntry : tInDeductionApplyEntryList)
            {
                tInDeductionApplyEntry.setDeductionId(deductionId);
                list.add(tInDeductionApplyEntry);
            }
            if (list.size() > 0)
            {
                tInDeductionApplyMapper.batchTInDeductionApplyEntry(list);
            }
        }
    }


}
